package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Department;
import com.liushuai.eas.pojo.Major;
import com.liushuai.eas.service.DepartmentImp;
import com.liushuai.eas.service.MajorImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/major")
public class MajorController {

    @Resource
    private MajorImp majorImp;

    @Resource
    private DepartmentImp departmentImp;

    @PostMapping
    public Result<?> add(@RequestBody Major major){

        //插入major数据库
        if (majorImp.insert(major)!=1)
            return Result.fail(major);

        return Result.success(major);
    }

    @PutMapping
    public Result<Major> update(@RequestBody Major major){
        if (majorImp.updateById(major)!=1)
            return Result.fail(major);
        return Result.success(major);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        List<Major> majorList;
        int total = 0;
        HashMap<String,Object> map = new HashMap<String,Object>();

        if (search.isEmpty()){
            majorList = majorImp.queryMajorList((pageNum - 1) * pageSize, pageSize);
            total = majorImp.queryTotal();
        }else {
            majorList = majorImp.queryMajorListBySearch((pageNum - 1) * pageSize, pageSize,search);
            total = majorImp.queryTotalBySearch(search);
        }

        for(Major major : majorList){
            major.setDepartment(departmentImp.selectOne(major.getDepartmentId()));
        }

        map.put("records",majorList);
        map.put("total",total);
        map.put("pageSize",pageSize);
        return Result.success("数据加载成功",map);
    }

    @DeleteMapping
    public Result<Major> delete(@RequestBody Major major){
        if (majorImp.deleteById(major)!=1)
            return Result.fail(major);
        return Result.success(major);
    }

    @GetMapping("/selectAll")
    public Result<List<Major>> selectAll(){
        List<Major> majorList = majorImp.queryMajorListAll();

        return Result.success(majorList);
    }

    @GetMapping("/selectOne")
    public Result<Major> selectOne(@RequestParam Integer majorId){
        Major major = majorImp.selectOne(majorId);
        major.setDepartment(departmentImp.selectOne(major.getDepartmentId()));

        return Result.success(major);
    }

    @GetMapping("/majorCount")
    public Result<?> majorCount(){
        int total = 0;
        HashMap<String,Object> map = new HashMap<String,Object>();
        total = majorImp.queryTotal();
        map.put("id",2);
        map.put("name","专业数量");
        map.put("icon","el-icon-s-management");
        map.put("total",total);
        return Result.success("数据加载成功",map);
    }

}
